<template>
  <div class="dictionaries">
  </div>
</template>

<script>
  import {
    getTSTypePlus,
    BusinessTable
  } from "@/api/dma/dictionaries/index";
export default {
    name:'dictionaries',
    props:{
        searchData:{
            type: Array,
            required: true
        },
        modelType:{
            type: String,
            required: true
        },
        newData:{

        }
    },
    data(){
        return{
            dictionaryData:{
                dictionaryObj:{},
                // 字典分组编码
                typeGroupCode:''
            } ,
            // 转换后的options数据
            optionData:[],
            // option的假数据
            option: [],
              list:{}
        }
    },
    mounted() {
        if (this.searchData) {
            const searchDataT = JSON.stringify(this.searchData)
            if (searchDataT) {     
                this.dictionaryData.dictionaryObj = JSON.parse(JSON.stringify(this.searchData));
            }  
        }      
    },
    methods:{
        keyValue(beforeObj, afterObj){
            let list = this.list
        // @ts-ignore
            beforeObj[afterObj['EN']] = list[afterObj['code']]
            return beforeObj
        },
        // 字典表查询   |    跨表查询
        // 字典表查询
        getTypeGroupCode(){
           return new Promise(async (resolve, reject) => {
                let searchData = this.searchData
                // 设置code 的数组
                let codeArr = []
                let ENArr = []                
            // 设置一个空对象
            // var Obj = {}
            for (let i = 0; i < searchData.length; i++) {
                // 遍历后获取字典编码
                let typeGroupCode = searchData[i].typeGroupCode            
                // 遍历后获得每一个的EN值
                if (typeGroupCode) {
                    codeArr.push(typeGroupCode)
                    // console.log(codeArr,357);
                    let index = i
                    let EN = searchData[index].EN 
                    let enobj = {}
                    enobj.EN = EN        
                    enobj.code = typeGroupCode        
                    ENArr.push(enobj)
                }
            }         
            const res = await getTSTypePlus(codeArr)
            this.list = res.data
            const newObj = ENArr.reduce(this.keyValue, {})
            // console.log(newObj, 63635)
            resolve(newObj)
           })            
        },
        // 跨表查询
        getTypeGroupCodeAcross(){
           return new Promise(async (resolve, reject) => {
                let searchData = this.searchData
            let objAcross = {}       
            for (let i = 0; i < searchData.length; i++) {
                if (searchData[i].dictTable) {
                    let table = searchData[i].dictTable.table
                    let id = searchData[i].dictTable.id
                    let name = searchData[i].dictTable.name
                    let associated = searchData[i].dictTable.associated
                    if (!associated) {
                        associated = ''
                    }
                    let index = i
                    let EN = searchData[index].EN 
                    // console.log(table,id,name,associated);            
                    const res = await BusinessTable(table,id,name,associated)                
                    searchData[i].option = optionDatas
                    let optionDatas = res.data
                    objAcross[EN] = optionDatas           
                }
            }
                resolve(objAcross)
           })           
        },  
        // 传递optionData
        sendOptionData(option){           
            this.$emit('optionData',option)
        },

    },
    async created(){
        const result = await Promise.all([this.getTypeGroupCode(),this.getTypeGroupCodeAcross()].map(p => p.catch(e => console.log(e))))
        this.sendOptionData(result)  
    }
}
</script>

<style>

</style>